package com.example.qxfw.nyqx.service.impl;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.ny.CropDistIndex;
import com.example.qxfw.common.entity.ny.StationInfo;
import com.example.qxfw.nyqx.service.CropDistIndexService;
import com.example.qxfw.nyqx.service.NybchService;
import com.example.qxfw.nyqx.service.SumSunDayDataService;
import com.example.qxfw.nyqx.vo.CropDistVo;
import com.example.qxfw.rcyw.service.SmallScaleStationService;
import com.example.qxfw.rcyw.service.StationInfoService;
import com.example.qxfw.ybzz.service.TownshipForecastService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class NybchServiceImpl implements NybchService {

    @Autowired
    private CropDistIndexService cropDistIndexService;

    @Autowired
    private StationInfoService stationInfoService;

    @Autowired
    private TownshipForecastService townshipForecastService;

    @Autowired
    private SmallScaleStationService smallScaleStationService;

    @Autowired
    private SumSunDayDataService sumSunDayDataService;

    @Override
    public List<CropDistIndex> listDistIndex(String cropName) {
        return null;
    }

    @Override
    public SysResult ybzz(String cropName, String time) throws ParseException {
        List<CropDistVo> cropDistVos = new ArrayList<>();

        SimpleDateFormat sdf = new SimpleDateFormat("MMdd");

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        Date parse = sdf1.parse(time);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parse);
        calendar.add(Calendar.DATE,-1);
        String dateChar = sdf1.format(calendar.getTime());

        //查询病虫害指标
        List<CropDistIndex> cropDistIndices = cropDistIndexService.getList(cropName, sdf.format(parse));

        //获取农服站点
        List<StationInfo> stationInfos = stationInfoService.listByMainCrop(cropName);
        if (stationInfos.size()==0){
            return SysResult.fail("没有农服站点主要农作物为"+cropName);
        }

        for (CropDistIndex cropDistIndex : cropDistIndices) {
            int conditionNum = 0;
            CropDistVo cropDistVo = new CropDistVo();

            for (StationInfo stationInfo : stationInfos) {
                //温度-获取温度预报
                String stationId = "";
                List<Double> maxTemps = townshipForecastService.getMaxTemp(dateChar, "24", "20", stationInfo.getStationid());
                if (maxTemps.size()==0){
                    return SysResult.fail("农服站点没有"+dateChar+"的预报数据");
                }
                for (Double maxTemp : maxTemps) {
                    if (cropDistIndex.getMaxtemp()>maxTemp&&cropDistIndex.getMintemp()<maxTemp){
                        conditionNum++;
                    }
                }
                //湿度
                List<Double> humids = townshipForecastService.getAvgHumid(dateChar, "24", "20", stationInfo.getStationid(),cropDistIndex.getMinhumidity(),cropDistIndex.getMaxhumidity());
                if (humids.size()>0){
                    conditionNum++;
                }
                //日照数
                if (cropDistIndex.getSunhour()>0){
                    List<String> countryByStationId = smallScaleStationService.getCountryByStationId(stationInfo.getStationid());
                    String county = countryByStationId.get(0);
                    if (county.equals("顺庆") || county.equals("嘉陵")){
                        county = "高坪";
                    }
                    List<String> stationIds = smallScaleStationService.getStationId(county);
                    if (stationIds.size()>0){
                        stationId = stationIds.get(0);
                    }

                    List<String> sumValue = sumSunDayDataService.getSumValue(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), stationId);
                    if (sumValue.size()>0){
                        double sun = Double.parseDouble(sumValue.get(0));
                        if (sun>cropDistIndex.getSunhour()){
                            conditionNum++;
                        }
                    }
                } else {
                    conditionNum++;
                }
                if (conditionNum==3){
                    cropDistVo.setCropName(cropDistIndex.getCropname());
                    cropDistVo.setPestName(cropDistIndex.getDiseasesname());
                    cropDistVo.setMeasure(cropDistIndex.getDefendmeasure());
                    cropDistVo.setStartTime(cropDistIndex.getStarttime());
                    cropDistVo.setEndTime(cropDistIndex.getEndtime());
                    cropDistVo.setEasyTime(cropDistIndex.getEasyhappentime());
                    cropDistVo.setMinTemp(cropDistIndex.getMintemp());
                    cropDistVo.setMaxTemp(cropDistIndex.getMaxtemp());
                    cropDistVo.setMinHumid(cropDistIndex.getMinhumidity());
                    cropDistVo.setMaxHumid(cropDistIndex.getMaxhumidity());
                    cropDistVo.setSunCount(cropDistIndex.getSunhour());
                    cropDistVo.setStationId(stationInfo.getStationid());
                    cropDistVo.setStationName(stationInfo.getStationname());
                    cropDistVos.add(cropDistVo);
                }

            }


        }

        if (cropDistVos.size()==0){
            return SysResult.fail("该作物在当前时间无病虫害");
        }

        return SysResult.success(cropDistVos);
    }
}
